package com.wisdom.benefit.jufa.utils;

import android.app.Activity;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 输入法工具类
 * Created by zhoudawei on 2017/1/13.
 */

public final class InputMethodUtil {

    /**
     * 隐藏输入法
     *
     * @param activity 传入一个activity
     */
    public static void hideInputMethod(@Nullable Activity activity) {
        if (activity == null) return;
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 显示输入法
     *
     * @param activity 传入一个activity
     * @param editText 需要调用输入法的控件
     */
    public static void showInputMethod(@Nullable Activity activity, @NonNull EditText editText) {
        if (activity == null) return;
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            editText.requestFocus();
            imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
        }
    }
}
